﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels/MainWindowViewModel
* 类       名: MainWindowViewModel
* 功      能:  TD
* 时      间:  2024-04-25 15:41:27
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using WaferSelectV3.Common;
using WaferSelectV3.Dialogs;
using WaferSelectV3.Models;
using WaferSelectV3.Services;
using WaferSelectV3.Views;

namespace WaferSelectV3.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private IRegionManager _regionManager;
        public MainWindowViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;
            // 导航菜单初始化
            InitMenubars();

            NavigateCommand = new DelegateCommand<MenuBar>(DoNAvigateCommand);
            SetParamsCommand = new DelegateCommand(DoSetParams);
            ChangeUserCommand = new DelegateCommand(DoChangeUser);
            AppExitComamnd = new DelegateCommand(DoAppExit);

            // 默认登录用户为Guest(操作员)
            CurrentLoginUser = $"{GlobalValues.CurrentLoginUser.UserName}({Enum.GetName(typeof(RoleType), (int)RoleType.操作员)})";
            // 定时器显示底部菜单当前的时间
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += UpdateUiShowTime;
            timer.Start();
        }

        private void UpdateUiShowTime(object? sender, EventArgs e)
        {
            SystemTime = DateTimeHelper.GetUiShowDateTime();
        }

        private string? _currentLoginUser;
        /// <summary>
        /// 当前登录的用户 username(role)
        /// </summary>
        public string? CurrentLoginUser
        {
            get { return _currentLoginUser; }
            set
            {
                _currentLoginUser = value;
                RaisePropertyChanged();
            }
        }

        private string? _systemTime;

        public string? SystemTime
        {
            get { return _systemTime; }
            set
            {
                _systemTime = value;
                RaisePropertyChanged();
            }
        }



        /// <summary>
        /// 参数设置命令
        /// </summary>
        public DelegateCommand SetParamsCommand { get; private set; }
        private void DoSetParams()
        {
            IDialogParameters parameters = new DialogParameters()
            {
                {"Title","参数设置" }
            };
            DialogProvider.Show(nameof(SetParametersDialog), parameters);
        }
        /// <summary>
        /// 切换用户命令
        /// </summary>
        public DelegateCommand ChangeUserCommand { get; private set; }
        private void DoChangeUser()
        {
            // TODO 切换用户逻辑
        }

        public DelegateCommand AppExitComamnd { get; private set; }
        private void DoAppExit()
        {
            // TODO 退出程序命令
        }

        /// <summary>
        /// 导航命令
        /// </summary>
        public DelegateCommand<MenuBar> NavigateCommand { get; private set; }
        private void DoNAvigateCommand(MenuBar menuBar)
        {
            if (menuBar != null && !string.IsNullOrWhiteSpace(menuBar.ViewName))
            {
                _regionManager.Regions[RegionNameManager.MainRegionContent].RequestNavigate(menuBar.ViewName);
                PageTitle = menuBar.Title;
            }
        }

        // 显示页面标题使用
        private string? _pageTitle;

        public string? PageTitle
        {
            get { return _pageTitle; }
            set
            {
                _pageTitle = value;
                RaisePropertyChanged();
            }
        }

        private MenuBar? _menubarSelected;

        public MenuBar? MenuBarSelected
        {
            get { return _menubarSelected; }
            set
            {
                _menubarSelected = value;
                RaisePropertyChanged();
            }
        }


        private ObservableCollection<MenuBar> _menuBars = new ObservableCollection<MenuBar>();

        public ObservableCollection<MenuBar> MenuBars
        {
            get { return _menuBars; }
            set
            {
                _menuBars = value;
                RaisePropertyChanged();
            }
        }

        private void InitMenubars()
        {
            MenuBars.Clear();
            MenuBars.Add(new MenuBar() { Title = "主控", ViewName = nameof(IndexView) });
            MenuBars.Add(new MenuBar() { Title = "配方设置", ViewName = nameof(RecipeSettingView) });
            MenuBars.Add(new MenuBar() { Title = "结果汇总", ViewName = nameof(ResultSummaryView) });
            MenuBars.Add(new MenuBar() { Title = "从站控制", ViewName = nameof(SlaveControlView) });
            MenuBars.Add(new MenuBar() { Title = "报警日志", ViewName = nameof(AlarmLogView) });
            MenuBars.Add(new MenuBar() { Title = "结果补偿", ViewName = nameof(VisionOffsetView) });
            MenuBars.Add(new MenuBar() { Title = "历史查询", ViewName = nameof(HistoryQueryView) });
            MenuBars.Add(new MenuBar() { Title = "用户管理", ViewName = nameof(UserManagementView) });
            MenuBarSelected = MenuBars.FirstOrDefault();
            PageTitle = MenuBarSelected?.Title;
        }
    }
}
